//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<SceneManager.h>
///	@path	~/src/scene/
///	@date	2007/10/11
///	@desc	Scene Manager.

#pragma once

#include "xeres.h"

#include <vector>

#include "lib/utilities/dict.h"
#include "lib/system/lock.h"

#include "database/IData.h"
#include "database/DataManager.h"

#include "graphics/RenderContext.h"

#include "scene/IScene.h"
#include "scene/ICamera.h"
#include "scene/ScenePresentation.h"
#include "scene/cull/Culler.h"
#include "scene/resource/ResourceLoader.h"

namespace xeres {

	/*!
		\class	SceneManager
		\brief
	*/
	class SceneManager : public Culler
	{
	protected:

		/// \name Constructor & Destructor
		//@{

		/// \ctor
		SceneManager( void );

		/// \dtor
		~SceneManager( void );

		//@}

		/// \name Internal Manipulation
		//@{

		/*!
			\brief		Initialize scene manager.
		*/
		void Initialize( ResourceLoader * loader , RefWeak<IData> config );

		/*!
			\brief		Finalize scene manager.
		*/
		void Finalize( void );

		/*!
			\brief		Determine if scene is initialized.
		*/
		bool IsInitialized( void ) const { return m_inited; }

		/*!
			\brief		Pre-load specific scene into memory & get ready to construct structure.
		*/
		bool PreLoadScene( const WString& name );

		/*! 
			\brief		Active scene by name.
		*/
		bool ActiveScene( const WString& name );

		/*!
			\brief		Update scene(s).
		*/
		void Tick( const Frame& frame );

		/*!
			\brief		Present active scene.
		*/
		void Present( RenderManager& rm );

		//@}

	public:

		/// \name Interface Manipulation
		//@{

		/*! 
			\brief		Camera scene.
		*/
		void SetCamera( RefWeak<ICamera> cam );

		/*! 
			\brief		Get camera of scene.
		*/
		RefWeak<ICamera> GetCamera( void );

		//@}

	private:

		friend class Engine;

		struct _SceneNote
		{
			// scene state
			int						state;

			// scene operation lock
			SpinLock				lock;

			// scene instance
			Ref<IScene>				scene;

			// scene class name
			WString					className;

			// scene resource path
			WString					path;
		};

	//@ data

		// status
		bool								m_inited;
		bool								m_spare0[3];

		// resource loader
		ResourceLoader *					m_loader;

		// active camera
		Ref<ICamera>						m_camera;

		// active scene
		RefWeak< IScene >					m_active;

		// scenes
		dict< WString , _SceneNote >		m_scenes;

	};

} // namespace xeres
